import os

import requests

from common.conf import data_path
from common.config import ip
from APIs.baseApi import BaseAPI
from APIs.loginApi import LoginApi
from common.doDB import DoDB


class LogApi(BaseAPI):
    header={"Admin-Token":""}
    def fileUpload(self,fname):
        fData1 = {'type': 'img'}
        # fname = r'D:\2504\apiTest\datas\1.png'
        fname=os.path.join(data_path,fname)
        fData2 = {'file':(fname, open(fname, 'rb'),'image/png')}
        r2 = requests.post(f"{ip}/file/upload", data=fData1, files=fData2, headers=self.header)
        print(r2.json())
        # 获取batchId
        ebatchId = r2.json()['batchId']
        return ebatchId

    def addLog(self,add):
        #新增日志
        # addLogData = {"categoryId": "1", "content": "1", "tomorrow": "2", "question": "3", "batchId": ebatchId,
        #               "sendUserIds": "", "sendDeptIds": "", "customerIds": "", "contactsIds": "", "businessIds": "",
        #               "contractIds": ""}
        r3 = requests.post(f"{ip}/OaLog/addOrUpdate", json=add, headers=self.header)
        print(r3.json())
        return r3


    def getLog(self,get={'page': 1, 'limit': 5, 'by': 1}):
        # 查看log日志
        # getLogData = {'page': 1, 'limit': 5, 'by': 1}
        r1 = requests.post(f"{ip}/OaLog/queryList", json=get, headers=self.header)
        # print(r1.json())
        return r1
        # 获取logId
        # eId = r1.json()['data']['list'][0]['logId']
        # return eId


    def updataLog(self,up):
        #修改日志
        # upLogData = {"logId": eId, "categoryId": "1", "content": "好123", "tomorrow": "很好456",
        #              "question": "非常好789", "batchId": ebatchId, "sendUserIds": "", "sendDeptIds": "",
        #              "customerIds": "", "contactsIds": "", "businessIds": "", "contractIds": ""}
        r4 = requests.post(f"{ip}/OaLog/addOrUpdate", json=up, headers=self.header)
        # print(r4.json())
        return r4

    def deleteLog(self,dell):
        # 删除log日志
        # delLogData = {"logId": eId}
        r5 = requests.post(f"{ip}/OaLog/deleteById", data=dell, headers=self.header)
        # print(r5.json())
        return r5



    def getlogIds(self):
        r = self.getLog()
        ddl = r.json()["data"]["list"]
        ddlIds = []
        for i in ddl:
            ddlIds.append(i["logId"])

        return ddlIds


    def getLogidsBYSQL(self,sql):
        #to-do
        db=DoDB()
        t = db.doselectSQL(sql)
        s=[]
        for i in t:
            s.append(i[0])
        return s

    def claerLogs(self):
        #获取所有ids
        ddl=self.getlogIds()
        ddlIds=[]
        for i in ddl:
            delLogData = {"logId":i["logId"]}
            self.deleteLog(delLogData)


if __name__=='__main__':
    # r=LoginApi().login()
    # token=r.json()['Admin-Token']
    #
    # lapi=LogApi(token)
    # # lapi.header['Admin-Token']=token
    #
    # # bid=lapi.fileUpload()
    # # lapi.addLog(bid)
    # lapi.getLog()
    a=LogApi()
    sql="SELECT log_id FROM 72crm_oa_log " \
        "WHERE create_user_id = " \
        "(SELECT user_id FROM 72crm_admin_user WHERE username='admin')";
    r=a.getLogidsBYSQL(sql)
    print(r)
